package com.zero.zeroframe.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import com.lxj.androidktx.AndroidKTX
import com.lxj.androidktx.core.gone
import com.lxj.androidktx.core.visible
import com.zero.zeroframe.R
import com.zero.zeroframe.databinding.PopupLoadingBinding
import com.zero.zeroframe.v.BasePopup

class LoadingPopup(context: Context): BasePopup<PopupLoadingBinding>(context, R.layout.popup_loading){

    fun show(msg:String = ""){
        if (msg != ""){
            mBinding.msg.text = msg
            mBinding.msg.visible()
        }else{
            mBinding.msg.gone()
        }
        showPopupWindow()
    }

    fun hide(){
        dismiss()
    }

    override fun PopupLoadingBinding.initView() {
        popupGravity = Gravity.CENTER
        setBackground(0)
        setOutSideDismiss(false)
    }

    companion object{
        private var loadingPopup : LoadingPopup?=null
        fun show(msg:String = ""): LoadingPopup {
            if (loadingPopup == null || !loadingPopup!!.isShowing){
                loadingPopup = LoadingPopup(AndroidKTX.context)
                loadingPopup!!.show(msg)
            }
            return loadingPopup!!
        }

        fun hide(){
            loadingPopup?.dismiss()
        }
    }

    override fun bindView(contentView: View) {
        mBinding = PopupLoadingBinding.bind(contentView)
    }
}